package com.primeton.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;

public class SocketServer extends Thread {

	@Override
	public void run() {
		while (true) {
			try {
				Socket socket = server.accept();
				InputStream is = socket.getInputStream();
				Input input = new Input(is);
				System.out.println(new String(input.getBuffer()));

				Kryo kryo = new Kryo();
				kryo.register(Fick.class);
				Fick fick = kryo.readObjectOrNull(input, Fick.class);
				System.out.println(fick.getName());
				System.out.println(fick.getAge());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private static final int port = 60001;

	private ServerSocket server;

	public SocketServer() {
		try {
			server = new ServerSocket(port);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("open socket server error!");
		}
	}

}
